let apis = {};

const moduleNameReg = /\.\/(?<name>[^.]*)\.js/;

const checkUnique = (keys, api) => {
  const apisKey = Object.keys(apis);
  const duplicateKey = keys.find(item1 => ~apisKey.findIndex(item2 => item2 === item1));
  if (!duplicateKey) return true;
  const moduleName = moduleNameReg.exec(api).groups.name;
  console.error(`http请求模块: ${moduleName} 中的 ${duplicateKey} 方法重复,请检查...`);
};

const apiContext = require.context('../api/', true, /\.js$/);
apiContext.keys().forEach(api => {
  const apiModule = apiContext(api);
  const keys = Object.keys(apiModule);
  if (keys.length) {
    checkUnique(keys, api);
    apis = {
      ...apis,
      ...apiModule
    };
  }
});

export default apis;
